phpDocumentor Web Commons
[ class tree: Web Commons ] [ index: Web Commons ] [ all elements ]

Source for file config.php

Documentation is available at config.php

  1. <?php
  2. /**
  3.  * This file groups classes that are used to manage configuration.
  4.  * 
  5.  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
  6.  * @license http://www.opensource.org/licenses/mit-license.php
  7.  * @version 0.1dev
  8.  */
  9.  
  10. /**
  11.  * Provides an access to configuration sections and items.
  12.  * @since 0.1
  13.  */
  14. interface Config {
  15.     /**
  16.      * Returns an array containing values defined inside $section
  17.      * @param string $section 
  18.      * @return mixed[string] 
  19.      * @throws MisconfigurationException
  20.      * @since 0.1
  21.      */
  22.     function getSection($section);
  23.     
  24.     /**
  25.      * Returns a configuration $item from a $section
  26.      * @param string $section 
  27.      * @param string $item 
  28.      * @return mixed 
  29.      * @throws MisconfigurationException
  30.      * @since 0.1
  31.      */
  32.     function getItem($section$item);
  33. }
  34.  
  35. /**
  36.  * A configuration built with a .ini file.
  37.  * @since 0.1
  38.  */
  39. class IniFileConfig implements Config {
  40.     /**
  41.      * Holds the configuration items and sections.
  42.      * @var string[string][string] 
  43.      */
  44.     private $config;
  45.     
  46.     /**
  47.      * Throws a MisconfigurationException if the file is missing, could not be
  48.      * read or parsed.
  49.      * 
  50.      * @param string $filename path to config file.
  51.      * @throws MisconfigurationException
  52.      * @since 0.1
  53.      */
  54.     public function __construct($filename{
  55.         $this->config load_extensible_ini_file($filenametrue);
  56.         
  57.         if ($this->config === false{
  58.             throw new MisconfigurationException(
  59.                 'Could not read or parse config file at ' $filename
  60.             );
  61.         }
  62.     }
  63.     
  64.     /**
  65.      * (non-PHPdoc)
  66.      * @see WebCommons/Config::getItem()
  67.      */
  68.     public function getItem($section$item{
  69.         $section self::getSection($section);
  70.         
  71.         if (array_key_exists($item$section)) {
  72.             return $section[$item];
  73.         }
  74.         else {
  75.             throw new MisconfigurationException(
  76.                 'No such configuration item: ' $item);
  77.         }
  78.     }
  79.     
  80.     /**
  81.      * (non-PHPdoc)
  82.      * @see WebCommons/Config::getSection()
  83.      */
  84.     public function getSection($section{
  85.         if (array_key_exists($section$this->config)) {
  86.             return $this->config[$section];
  87.         }
  88.         else {
  89.             throw new MisconfigurationException(
  90.                 'No such configuration section: ' $section);
  91.         }
  92.     }
  93. }
  94.  
  95. /**
  96.  * Exception thrown when the configuration could not be read
  97.  * or when a requested configuration item is missing.
  98.  * @since 0.1
  99.  */
  100. class MisconfigurationException extends Exception {}

Documentation generated on Fri, 16 Jul 2010 00:48:39 +0200 by phpDocumentor 1.4.3